<?php

namespace app\service\pixel;

use app\model\PixelLogs;
use FacebookAds\Object\ServerSide\ActionSource;
use FacebookAds\Object\ServerSide\Content;
use FacebookAds\Object\ServerSide\CustomData;
use FacebookAds\Object\ServerSide\DeliveryCategory;
use FacebookAds\Object\ServerSide\Event;
use FacebookAds\Object\ServerSide\UserData;
use think\helper\Arr;

trait FacebookTrait
{
    protected function createPurchaseEvent(PixelLogs $log)
    {
        $params = $log->params;
        $order = Arr::get($params,'order',[]);
        $address = Arr::get($params,'address',[]);
        $products = Arr::get($params,'products',[]);
        if (empty($params)) throw new \Exception("params is empty");
        $user_data = new UserData();
        $user_data->setClientIpAddress($log->ip);
        if($log->user_agent)$user_data->setClientUserAgent($log->user_agent);
        if($first_name = Arr::get($address,'first_name', '')) $user_data->setFirstName($first_name);
        if($last_name = Arr::get($address,'last_name', '')) $user_data->setLastName($last_name);
        if($email = Arr::get($address,'email', '')) $user_data->setEmail($email);
        if($city = Arr::get($address,'city', '')) $user_data->setCity($city);
        if($state = Arr::get($address,'state', '')) $user_data->setState($state);
        if($country_code = Arr::get($address,'country_code', '')) {
            $country_code = $country_code == "C2" ? "CN" : $country_code;
            $user_data->setCountryCode($country_code);
        }
        if($zip = Arr::get($address,'zip', '')) $user_data->setZipCode($zip);
        if($phone = Arr::get($address,'phone', '')) $user_data->setPhones([$phone]);
        if ($log->fbclid) {
            $fbc_at = $log->fbc_at;
            $fbc = sprintf("fb.1.%s.%s", $fbc_at, $log->fbclid);
            $user_data->setFbc($fbc);
        }
        $content = (new Content())
            ->setProductId(Arr::get($products,'product_title'))
            ->setQuantity(Arr::get($products,'quantity'))
            ->setDeliveryCategory(DeliveryCategory::HOME_DELIVERY);

        $custom_data = (new CustomData())
            ->setContents(array($content))
            ->setCurrency(Arr::get($order,'currencyCode','USD'))
            ->setValue(0 + Arr::get($order,'amount',0));
        return (new Event())
            ->setEventName('Purchase')
            ->setEventId(Arr::get($order,'id',uniqid()))
            ->setEventTime(time())
            ->setEventSourceUrl($log->website)
            ->setUserData($user_data)
            ->setCustomData($custom_data)
            ->setActionSource(ActionSource::WEBSITE);
    }

    /**
     * @param PixelLogs $log
     * @return Event
     * @DESC 创建异步事件
     */
    protected function createViewContentEvent(PixelLogs $log): Event
    {
        $user_data = (new UserData())
            // It is recommended to send Client IP and User Agent for Conversions API Events.
            ->setClientIpAddress($log->ip)
            ->setClientUserAgent($log->user_agent);
        if ($log->fbclid) {
            $fbc_at = $log->fbc_at;
            $fbc = sprintf("fb.1.%s.%s", $fbc_at, $log->fbclid);
            $user_data->setFbc($fbc);
        }
        return (new Event())
            ->setEventName('ViewContent')
            ->setEventTime(time())
            ->setEventSourceUrl($log->website)
            ->setUserData($user_data)
            ->setActionSource(ActionSource::WEBSITE);
    }
}